package com.example.hasee.zhcd.Activities.XiaoXi;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hasee.zhcd.Base.BaseFragment;
import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.Bean.MsgItemInfo;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.GlobalParameter;
import com.example.hasee.zhcd.Util.ProgressDialogUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import static android.view.View.Z;

/**
 * Created by hasee on 2018/7/9.
 */

public class WeiMsgFragment extends BaseFragment {
    private static DataListener mDataListener;
    private CheckBox quanxuan;
    private TextView biaoweiyidu;
    private HashMap<String, String> loginmap = (HashMap<String, String>) GlobalParameter.get("loginmap");
    private static Context mContext;
    private ListView wei_list;
    public static ArrayList<MsgItemInfo> no_read_list = new ArrayList<MsgItemInfo>();
    private WeiAdapter adapter;
    private String fidString = "";
    private ArrayList<Boolean> bol_list = new ArrayList<Boolean>();
    private String data = "0";

    static WeiMsgFragment newInstance(Context context) {
        WeiMsgFragment newFragment = new WeiMsgFragment();
        mContext = context;
        return newFragment;
    }

    @Override
    public void getEventData(EventMsg msg) {
        switch (msg.getAction()) {
            case "Fail":
                ProgressDialogUtils.closeProgressDialog();
                Toast.makeText(getActivity(), "操作失败，请检查网络！", Toast.LENGTH_LONG).show();
                break;
            case "ZHMsg_NoReadList":
                Log.e("ZHMsg_NoReadList", msg.getMsg());
                ProgressDialogUtils.closeProgressDialog();
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    no_read_list.clear();
                    if ("0".equals(rspcode)) {
                        JSONArray data = json.getJSONArray("data");
                        for (int i = 0; i < data.length(); i++) {
                            MsgItemInfo msgItemInfo = new MsgItemInfo();
                            msgItemInfo.setSelected(false);
                            msgItemInfo.setFtitle(data.getJSONObject(i).getString("ftitle"));
                            msgItemInfo.setFdate(data.getJSONObject(i).getString("fdate"));
//                            msgItemInfo.setFguid(data.getJSONObject(i).getString("fguid"));
                            msgItemInfo.setFid(data.getJSONObject(i).getString("fid"));
                            no_read_list.add(msgItemInfo);
                        }
                        adapter = new WeiAdapter();
                        wei_list.setAdapter(adapter);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "ZHMsg_Renew":
                Log.e("ZHMsg_Renew", msg.getMsg());
                ProgressDialogUtils.closeProgressDialog();
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
//                    Toast.makeText(getActivity(), rspmessage, Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                getWeiMsgList();
                data = data + "1";
                mDataListener.onData(data);
                break;
        }
    }

    @Override
    public int contentViewId() {
        return R.layout.fragment_wei;
    }

    @Override
    public void set() {
        quanxuan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    for (int i = 0; i < no_read_list.size(); i++) {
                        no_read_list.get(i).setSelected(true);
                    }

                } else {
                    for (int i = 0; i < no_read_list.size(); i++) {
                        no_read_list.get(i).setSelected(false);
                    }
                }
                adapter = new WeiAdapter();
                wei_list.setAdapter(adapter);
            }
        });
        biaoweiyidu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bol_list.clear();
                fidString = "";
                for (int i = 0; i < no_read_list.size(); i++) {
                    bol_list.add(no_read_list.get(i).isSelected());
                }
                if (bol_list.contains(true)) {
                    for (int j = 0; j < bol_list.size(); j++) {
                        if (bol_list.get(j) == true) {
                            if ("".equals(fidString)) {
                                fidString = no_read_list.get(j).getFid();
                            } else {
                                fidString = fidString + "," + no_read_list.get(j).getFid();
                            }
                        } else {
                        }
                    }
                    setReaded();
                } else {
                    Toast.makeText(getActivity(), "您尚未选择可标记的未读消息", Toast.LENGTH_LONG).show();
                }

            }
        });
    }

    @Override
    public void init(View view) {
        quanxuan = (CheckBox) view.findViewById(R.id.all_box);
        biaoweiyidu = (TextView) view.findViewById(R.id.check);
        wei_list = (ListView) view.findViewById(R.id.wei_list);
    }

    private void setReaded() {
        ProgressDialogUtils.createProgressDialog(getActivity(), "正在提交数据...");
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("strPhone", loginmap.get("phone"));
        map.put("strFID", fidString);
        httpUtils.get("ZHMsg_Renew", map, "ZHMsg_Renew");
    }

    private void getWeiMsgList() {
        ProgressDialogUtils.createProgressDialog(getActivity(), "正在获取列表...");
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("strPhone", loginmap.get("phone"));
        httpUtils.get("ZHMsg_NoReadList", map, "ZHMsg_NoReadList");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e("参数", loginmap.get("phone"));
        getWeiMsgList();
    }

    @Override
    public void onClick(View view) {

    }

    public static void setOnDataListener(DataListener dataListener) {
        mDataListener = dataListener;
    }

    class WeiAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return no_read_list.size();
        }

        @Override
        public Object getItem(int i) {
            return null != no_read_list ? no_read_list.get(i) : null;
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {
            Holder holder = null;
            MsgItemInfo msgItemInfo = (MsgItemInfo) getItem(i);
            if (null == holder) {
                LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.msg_list_item, null);
                holder = new Holder();
                holder.msg_box = (CheckBox) view.findViewById(R.id.msg_box);
                holder.ftitle = (TextView) view.findViewById(R.id.ftitle);
                holder.fdate = (TextView) view.findViewById(R.id.fdate);
                holder.kuang = (RelativeLayout) view.findViewById(R.id.kuang);
                final Holder fholder = holder;
                holder.msg_box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        MsgItemInfo info = (MsgItemInfo) fholder.msg_box.getTag();
                        info.setSelected(compoundButton.isChecked());
                    }
                });
                view.setTag(holder);
                holder.msg_box.setTag(msgItemInfo);
            } else {
                holder = (Holder) view.getTag();
                holder.msg_box.setTag(msgItemInfo);
            }
            holder.ftitle.setText(msgItemInfo.getFtitle());
            holder.fdate.setText(msgItemInfo.getFdate());
            holder.msg_box.setChecked(msgItemInfo.isSelected());
            holder.kuang.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getActivity(), MessageDetailActivity.class);
                    intent.putExtra("fid", no_read_list.get(i).getFid());
//                    intent.putExtra("fguid", no_read_list.get(i).getFguid());
                    getActivity().startActivity(intent);
                }
            });
            return view;
        }

        class Holder {
            private TextView ftitle;
            private TextView fdate;
            private CheckBox msg_box;
            private RelativeLayout kuang;
        }
    }
}
